let ChatModel = require('../model').chatModel;
let DaoTool = require('../lib/daoTool');
exports.hasChat = function (conn,params) {
    return new Promise(function (resolve, reject) {
        conn.query('select * from chat where ChatNo=?;', [params.ChatNo], function (err, result) {
            printLog("=hasChat=", this.sql);
            if (err) reject(err);
            resolve(result)
        });
    });
};
exports.createChat = function (conn,params) {
    return new Promise(function (resolve, reject) {
        let sql = "insert into chat set ?";
        conn.query(sql, [DaoTool.generateModelObject(params, ChatModel, 'create')], function (err, result) {
            printLog("=createChat=", this.sql);
            if (err) reject(err);
            resolve(result)
        });
    });
};
exports.updateChat = function (conn,params) {
    return new Promise(function (resolve, reject) {
        let sql = "update chat set ? where ChatNo=?;";
        conn.query(sql, [DaoTool.generateModelObject(params, ChatModel, 'update'),params.ChatNo], function (err, result) {
            printLog("=createChat=", this.sql);
            if (err) reject(err);
            resolve(result)
        });
    });
};
exports.selectChat = function (conn,params,isCount) {
    return new Promise(function (resolve, reject) {
        let sql = " from chat as cht LEFT JOIN chat_people as chp on chp.ChatNo=cht.ChatNo LEFT JOIN user as u on u.openid = chp.openid LEFT JOIN my_option as myo on myo.openid=u.openid LEFT JOIN ta_option as tao on tao.openid = u.openid left join black_list bl on bl.BlackOpenId=? and bl.openid=u.openid left join black_list bl2 on bl2.BlackOpenId=u.openid and bl2.openid=? where u.openid!=? " +selectChatWhere(params.params)+ " and chp.IsDel='F' and cht.ChatNo in (select cht.ChatNo from chat as cht left JOIN chat_people as chp on chp.ChatNo=cht.ChatNo where chp.openid=?) order by cht.CreatedAt desc ";
        let selectSql = "select u.UserNo,u.openid,u.NickName,u.HeadImgUrl,u.Sincerity,u.City,u.Province,chp.NewMsg,chp.ChatNo,cht.CreatedAt,myo.MyField147 as Birthday,myo.MyField148 as Height,bl.BlackId as IsBlack,bl2.BlackId as BeBlack," + formatMatchingSqlStr(params.selfInfo) + sql +DaoTool.getPageParamsSqlStr(params.params)+";";
        let countSql = "select count(1) as num" + sql;
        conn.query(selectSql, [params.params.openid,params.params.openid,params.params.openid,params.params.openid], function (err, selectResult) {
            printLog("=selectChat err=",err);
            printLog("=selectChat=",this.sql);
            if (err) reject(err);
            if (!isCount) resolve(selectResult);
            conn.query(countSql,[params.params.openid,params.params.openid,params.params.openid,params.params.openid],function (err,countResult) {
                printLog("=selectChat err=",err);
                printLog("=selectChat=",this.sql);
                if (err) reject(err);
                resolve({data:selectResult,count:countResult[0].num});
            });
        });
    });
};
function selectChatWhere(params) {
    let sql = "";
    if(params.ChatListParams){
        sql += ' (u.UserNo like ' + mysqlHelper.escape('%' + params.ChatListParams + '%')
        +' or u.NickName like '+ mysqlHelper.escape('%' + params.ChatListParams + '%')+')'
    }
    if(sql)
        sql = ' and ' + sql;
    return sql;
}
function formatMatchingSqlStr(params) {
    try {
        let myo_sql = "";
        let tao_sql = "";
        for (let k = 0; k<= 68; k++) {
            let i = config.questionArr[k];
            if (!params["MyField" + i] || !params["TaField" + i])
                continue;
            if (myo_sql) {
                myo_sql += "+";
                tao_sql += "+";
            }
            myo_sql += '(case when myo.MyField' + i + ' is null then 0 else (matching(myo.MyField' + i + ',"' + params["TaField" + i] + '")) end)';
            tao_sql += '(case when tao.TaField' + i + ' is null then 0 else (matching("' + params["MyField" + i] + '",tao.TaField' + i + ')) end)';
        }
        myo_sql = "((" +(myo_sql?myo_sql:0)  + ")/68) as ToMe";
        tao_sql = "((" + (tao_sql?tao_sql:0) + ")/68) as ToTa ";
        return myo_sql + "," + tao_sql;
    } catch (err) {
        console.log(err);
    }
}